package org.zoomdev.zoom.rpc.client;

import org.apache.commons.codec.binary.Hex;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.zoomdev.zoom.rpc.MethodId;
import org.zoomdev.zoom.rpc.utils.MessageDecoder;
import org.zoomdev.zoom.serializer.Serializer;

import java.util.List;

public class RpcClientBytesToMessageDecoder extends MessageDecoder {

    private final ClientHandler handle;

    private final Serializer<List<MethodId>> serializer;

    public RpcClientBytesToMessageDecoder(ClientHandler handle, Serializer<List<MethodId>> serializer) {
        this.handle = handle;
        this.serializer = serializer;
    }

    private static final Log log = LogFactory.getLog(RpcClient.class);

    @Override
    protected Object createObject(int id, short func, byte[] bytes) {
        if (log.isDebugEnabled()) {
            log.debug("收到消息 id:" + id + " retfunc:" + func + " data:" + Hex.encodeHexString(bytes));
        }

        if (func == 0 && id == 0) {
            //全部
            List<MethodId> list = serializer.deserialize(bytes);
            return list;
        }
        Command command = new Command();
        command.bytes = bytes;
        command.id = id;
        command.func = func;
        return command;

    }
}
